package com.lsismo.core.base.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;

import com.lsismo.core.base.dao.GenericDao;
import com.lsismo.core.base.service.GenericManager;
import com.lsismo.core.base.util.PageBean;

/**
 * 所有Manager类的基类，包含常用的CRUD方法，如果需要自定义CRUD逻辑需要在该类的基础上进行扩展
 *
 * <p>使用XML在Spring上下文中的配置方法如下
 * <pre>
 *     &lt;bean id="userManager" class="com.dhcc.itsm.core.service.impl.GenericManagerImpl"&gt;
 *         &lt;constructor-arg&gt;
 *             &lt;bean class="com.dhcc.itsm.core.dao.hibernate.GenericDaoHibernate"&gt;
 *                 &lt;constructor-arg value="com.dhcc.itsm.core.model.User"/&gt;
 *                 &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
 *             &lt;/bean&gt;
 *         &lt;/constructor-arg&gt;
 *     &lt;/bean&gt;
 * </pre>
 *
 * @param <T> a 类型变量
 * @param <PK> 主键
 */
public class GenericManagerImpl<T, PK extends Serializable> implements GenericManager<T, PK> {
    /**
     * 用于所有子类的日志变量
     */
    protected final Log log = LogFactory.getLog(getClass());

    /**
     * GenericDao实例, 通过该类的构造函数设置
     */
    protected GenericDao<T, PK> dao;

    /**
     * 公共构造函数，用于创建新的GenericManagerImpl的
     * @param dao 用于持久化的GenericDao
     */
    public GenericManagerImpl(final GenericDao<T, PK> dao) {
        this.dao = dao;
    }

	public Session getCurrentSession() {
		return  this.dao.getCurrentSession();
	}

    /**
     * {@inheritDoc}
     * @throws Exception 
     * @throws Exception 
     */
    public List<T> getAll() throws Exception{
        return dao.getAll();
    }

    /**
     * {@inheritDoc}
     */
    public T get(PK id) {
        return dao.get(id);
    }
    
    /**
     * {@inheritDoc}
     */
    public int bulkUpdate(String hql ,Object ... value){
    	return dao.bulkUpdate(hql,value);
    }

    /**
     * {@inheritDoc}
     */
    public boolean exists(PK id) {
        return dao.exists(id);
    }

    /**
     * {@inheritDoc}
     */
    public T save(T object) {
        return dao.save(object);
    }

    /**
     * {@inheritDoc}
     */
    public void remove(PK id) {
        dao.remove(id);
    }
    
    /**
     * {@inheritDoc}
     */
    public void remove(T object) {
    	dao.remove(object);
    }
    /**
     * 该方法用于批量删除(但不发生级联删除)
     * 
     * @param ids
     * @param idName
     */
    public void remove(PK ids , String idName){
	   dao.remove(ids, idName);
    }
   
    /**
     * {@inheritDoc}
     */
    public void bulkRemove(final PK[] ids) {
    	dao.bulkRemove(ids);
    }
    
	/**
     * {@inheritDoc}
     */
    public List<T> findByExample(T object) {
    	return dao.findByExample(object);
    }
    

    /**
     * {@inheritDoc}
     */
    public List<T> findByProperty(String propertyName, Object value) {
    	return dao.findByProperty(propertyName, value);
    }
    
   
    /**
     * {@inheritDoc}
     */
    public List<T> find(String hql, Object... values) {
    	return dao.find(hql, values);
    }
  
  
    /**
     * {@inheritDoc}
     */
    public List<T> getAllDistinct() {
    	return dao.getAllDistinct();
    }
	

    /**
     * {@inheritDoc}
     */
    public List<T> findByNamedQuery(String queryName, Map<String, Object> queryParams) throws Exception {
    	return dao.findByNamedQuery(queryName, queryParams);
    }
    
    /**
     * {@inheritDoc}
     */
    public List<T> findPageAll(String hql,int first,int max) throws Exception{
    	return dao.findPageAll(hql,first,max);
    }
    
    public List<T> findPageAll(String hql,int first,int max,Object...values) throws Exception{
		return dao.findPageAll(hql,first,max,values);
    }
    
    public void findPageAll(PageBean pageBean) throws Exception{
    	 Object[] values = pageBean.getValues();
    	 int total = this.getInfosCount(pageBean.getQueryHql(),pageBean.getValues());
		 if(total>0){
			 pageBean.setTotalCount(total);
			 if(values!=null&&values.length>0){
				 pageBean.setResult(this.findPageAll(pageBean.getQueryHql(),pageBean.getStart(),pageBean.getLimit(),values)); 
			 }else{
				 pageBean.setResult(this.findPageAll(pageBean.getQueryHql(),pageBean.getStart(),pageBean.getLimit()));
			 }
			 
		 }else{
			 pageBean.setTotalCount(0);
		 }
		
    }
    
    /**
     * {@inheritDoc}
     */
    public int getInfosCount(String hqlQuery)throws Exception{
    	return dao.getInfosCount(hqlQuery);
    }
    
    /**
     * {@inheritDoc}
     */
    public int getInfosCount(String hqlQuery,Object...values)throws Exception{
    	if( values!=null && values.length>0 ) 
    		return dao.getInfosCount(hqlQuery,values);
    	else
    		return dao.getInfosCount(hqlQuery);
    }
	 /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public Object commonFindById(Class clazz, PK id) {
     
    	Object entity = dao.commonFindById(clazz, id);
  
    	return entity;
    }
}
